// ==========================================================================
// 云捷GO自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-07-26 15:16:14
// 生成路径: app/service/module/news/news_service.go
// 生成人：gfcms
// ==========================================================================
package news

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	newsModel "myblog/app/model/module/news"
	userService "myblog/app/service/system/user"
	"myblog/app/utils/convert"
	"myblog/app/utils/excel"
	"myblog/app/utils/page"
	"myblog/app/utils/upfile"
)

//根据主键查询数据
func SelectRecordById(id int64) (*newsModel.Entity, error) {
	entidy, err := newsModel.FindOne("id", id)
	if err != nil {
		return nil, err
	}
	return entidy, nil
}

//根据主键删除数据
func DeleteRecordById(id int64) bool {
	result, err := newsModel.Delete("id", id)
	if err == nil {
		affected, _ := result.RowsAffected()
		if affected > 0 {
			return true
		}
	}

	return false
}

//批量删除数据记录
func DeleteRecordByIds(ids string) int64 {
	idarr := convert.ToInt64Array(ids, ",")
	result, err := newsModel.Delete("id in (?)", idarr)
	if err != nil {
		return 0
	}

	nums, _ := result.RowsAffected()

	return nums
}

//添加数据
func AddSave(req *newsModel.AddReq, session *ghttp.Session) (int64, error) {
	var entity newsModel.Entity

	entity.Catid = req.Catid
	entity.Title = req.Title
	entity.Thumb = req.Thumb
	entity.Description = req.Description
	entity.Author = req.Author
	entity.Listorder = req.Listorder
	entity.Status = req.Status
	entity.Content = req.Content
	entity.Views = int(req.Views)
	entity.Zan = int(req.Zan)
	entity.Top = int(req.Top)
	entity.Indexshow = int(req.Indexshow)
	entity.CreateTime = gtime.Now()
	entity.CreateBy = ""
	user := userService.GetProfile(session)

	if user != nil {
		entity.CreateBy = user.LoginName
	}

	result, err := entity.Insert()
	if err != nil {
		return 0, err
	}

	id, err := result.LastInsertId()

	if err != nil || id <= 0 {
		return 0, err
	}
	return id, nil
}

//修改数据
func EditSave(req *newsModel.EditReq, session *ghttp.Session) (int64, error) {

	entity, err := newsModel.FindOne("id=?", req.Id)

	if err != nil {
		return 0, err
	}

	if entity == nil {
		return 0, gerror.New("数据不存在")
	}

	entity.Catid = req.Catid
	entity.Title = req.Title
	entity.Thumb = req.Thumb
	entity.Description = req.Description
	entity.Author = req.Author
	entity.Content = req.Content
	entity.Views = int(req.Views)
	entity.Zan = int(req.Zan)
	entity.Top = int(req.Top)
	entity.Indexshow = int(req.Indexshow)
	entity.Listorder = req.Listorder
	entity.Status = req.Status
	entity.UpdateTime = gtime.Now()
	entity.UpdateBy = ""

	user := userService.GetProfile(session)

	if user == nil {
		entity.UpdateBy = user.LoginName
	}
	result, err := entity.Save()
	if err != nil {
		return 0, err
	}
	rs, err := result.RowsAffected()
	if err != nil {
		return 0, err
	}
	return rs, nil
}

//根据条件查询数据
func SelectListAll(params *newsModel.SelectPageReq) ([]newsModel.Entity, error) {
	return newsModel.SelectListAll(params)
}

//根据条件分页查询数据
func SelectListByPage(params *newsModel.SelectPageReq) ([]newsModel.EntityExtend, *page.Paging, error) {
	return newsModel.SelectListByPage(params)
}

// 导出excel
func Export(param *newsModel.SelectPageReq) (string, error) {
	result, err := newsModel.SelectListExport(param)
	if err != nil {
		return "", err
	}

	head := []string{"", "栏目名称", "标题", "缩略图", "阅读量", "点赞数", "排序", "状态", "创建时间", "修改时间"}
	key := []string{"id", "catid", "title", "thumb", "views", "zan", "listorder", "status", "create_time", "update_time"}
	url, err := excel.DownlaodExcel(head, key, result)

	if err != nil {
		return "", err
	}

	return url, nil
}

//上传文件
func Upfile(files ghttp.UploadFiles) ([]string, error) {
	return upfile.Upfile(files)
}

//删除文件
func Delfile(url string) error {
	return upfile.Delfile(url)
}

//首页top3
func TopIndex(num int) ([]*newsModel.EntityExtend, error) {
	return newsModel.TopIndex(num)
}

//最新文章
func Indexshow(num int) ([]*newsModel.EntityExtend, error) {
	return newsModel.Indexshow(num)
}
